package org.lex.utils.logging.pattern;

import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.logging.LogRecord;

public abstract class TimeBasedPattern extends Pattern {
	public static enum Style {
		SHORT(DateFormat.SHORT), MEDIUM(DateFormat.MEDIUM), LONG(DateFormat.LONG), FULL(DateFormat.FULL), DEFAULT(DateFormat.DEFAULT);
		private int style = DateFormat.DEFAULT;

		public static Style parse(String string) {
			return Style.valueOf(string.toUpperCase());
		}

		public int getStyle() {
			return this.style;
		}

		private Style(int style) {
			this.style = style;
		}
	}

	private int style = Style.DEFAULT.getStyle();
	private Date date = null;

	public TimeBasedPattern(String pattern) {
		super(pattern);
		String[] styles = this.getStyles();
		if (styles.length > 1) {
			// only one style element supported
			throw new IllegalArgumentException("Too many date styles, " + "you can specify at most one style :" + Arrays.toString(styles));
		}
		if (1 == styles.length)
			this.style = Style.parse(styles[0]).getStyle();
		else
			this.style = Style.DEFAULT.getStyle();
		this.date = new Date();
	}

	public abstract DateFormat getDateFormat();

	public int getStyle() {
		return this.style;
	}

	@Override
	public String format(LogRecord record) {
		this.date.setTime(record.getMillis());
		return this.getDateFormat().format(date);
	}
}
